home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 4 / MacMania 4.toast / / Tools&Utilities / MathPad 2.4 / Examples / falling < prev    next >
Text File  |  1996-03-25  |  1KB  |  30 lines

  1. --This example simulates a falling object with air friction proportional to the square of its velocity. The technique used is inefficient compared with fancier methods for numerical solutions of differential equations, but it is very easy to program. See example "projectile" for a similar problem in two dimensions.
  2.  
  3. -- stepto(0) sets up initial conditions. Any other stop time runs the simulation from where it left off up until the new stop time (within dt). Since the plot calls for points in order, the simulation does not need to start over for each plotted point.
  4.  
  5. init = v:=0,t:=0  -- initial conditions
  6.  
  7. stepto(stop) = init when stop=0,
  8.                (v:=v+a*dt,
  9.                 t:=t+dt) while t<stop
  10.  
  11. f=m*g-k*v^2   -- gravity - air friction
  12. a=f/m
  13.             
  14. g=9.8        -- accelleration due to gravity
  15. k=2          -- friction coefficient
  16. m=300        -- mass of object
  17. dt=.05       -- time step for simulation
  18.  
  19. vsim(t1) = stepto(t1),v  --run to t1, return v
  20.  
  21. Xmin=0; Xmax=10  -- plot v vs time for 10 sec
  22. plot vsim(X)
  23.  
  24. -- compare to analytical solution
  25. van(t)=sqrt(m*g/k)*tanh(sqrt(g*k/m)*t)
  26. plot van(X)
  27.  
  28. -- hyperbolic tan. (Or use XFun "hyperbolic")
  29. tanh(u)=(exp(u)-exp(-u))/(exp(u)+exp(-u))
  30.